/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Library.Common;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author larabenito
 */
public class GUIOperations {
    
    public static void updateTextPane(JTextPane panel, String newText) {
        String text = panel.getText() + systemDate() + " " + newText + "\n";
        panel.setText(text);
    }
    
    public static void updateTextPane(JTextPane panel, ObjectInputStream input) {
        try {
            updateTextPane(panel, input.readObject().toString());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
     private static String systemDate() {
        String date;
        Date fecha = new Date();
        date = fecha.toString();
        return (" (" + date + ")");
    }
     
     public static DefaultTreeModel getTreeFinal(String directory, boolean isServer, ObjectInputStream input,
            ObjectOutputStream output) {
         if(!directory.equals("")){
            DefaultMutableTreeNode father = new DefaultMutableTreeNode(directory);
            removeNodes(father, directory, isServer, input, output);
            JTree tree = new JTree(father);
            DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
            return model;
         } else {
             return null;
         }
     }
     
     private static boolean removeNodes(DefaultMutableTreeNode node, String directoryPath, boolean isServer,
            ObjectInputStream input, ObjectOutputStream output) {
         node.removeAllChildren();
         return removeNodes(node, directoryPath, 2, isServer, input, output);
     }
     
     private static boolean removeNodes(DefaultMutableTreeNode node, String directoryPath, int index,
            boolean isServer, ObjectInputStream input, ObjectOutputStream output) {
         ArrayList<String> list;
        if (isServer) {
            list = createArrayListFromServer(directoryPath, input, output);
        } else {
            list = createArrayList(directoryPath);
        }
        if (index > 0) {
            for (int i = 0; i < list.size(); i++) {
                DefaultMutableTreeNode children = new DefaultMutableTreeNode(list.get(i));
                removeNodes(children, list.get(i), index - 1, isServer, input, output);
                node.add(children);
            }
        }
        return true;
     }
     
     private static ArrayList<String> createArrayListFromServer(String filePath, ObjectInputStream input,
            ObjectOutputStream output) {
        ArrayList<String> list = new ArrayList<>();
        try {
            //Se envía al servidor la orden de listar y el nodo padre
            OperationsRefactor.outputMessage(output, new Message("listdirectoryserver"));
            OperationsRefactor.outputMessage(output, new Message(filePath));
            Message message = (Message) input.readObject();
            while (message != null && !(message.getMessage()).equals("listOfDirectoriesEnd")) {
                list.add(message.getMessage());
                message = (Message) input.readObject();
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }
     
     private static ArrayList<String> createArrayList(String filePath) {
        ArrayList<String> list = new ArrayList<>();
        File f = new File(filePath);
        File[] fileList = f.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return true;
            }
        });
        if (fileList != null) {
            for (int i = 0; i < fileList.length; i++) {
                list.add(fileList[i].toString());
            }
        }
        return list;
    }
    
}
